//用户信息
import {
  defineStore
} from 'pinia';
import {
  reactive
} from 'vue'
import * as http from '@/utils/http'

export const useUserInfo = defineStore('userInfo', () => {
  const loginData = reactive({
    "accessToken": null,
    "refreshToken": null,
    "userId": null,
    "openid": null

  });
  const baseInfo = reactive({
    "nickname": null,
    "avatar": null,
    "mobile": null,
    "sex": null
  });
  
  async function getUserInfo() {
    if (!baseInfo.mobile) {
      //从本地存储种获取
      let res = uni.getStorageSync("baseInfo");
      if (!res) {
        //跳转到登录页
        uni.showModal({
          title: '登录提示',
          content: '您还未登录，登录查看更多信息',
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
      } else {
        baseInfo.avatar = res.avatar
        baseInfo.nickname = res.nickname
        baseInfo.mobile = res.mobile
        baseInfo.sex = res.sex
      }
    }
  }

  async function getLoginInfo() {
    //获取用户登录信息，
    if (!loginData.userId) {
      //从本地存储种获取
      let res = uni.getStorageSync("loginData");
      if (!res) {
        //跳转到登录页
        uni.showModal({
          title: '登录提示',
          content: '您还未登录，登录查看更多信息',
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
      } else {
        loginData.accessToken = res.accessToken
        loginData.refreshToken = res.refreshToken
        loginData.userId = res.userId
        loginData.openid = res.openid
      }
    }
  }
  return {
    loginData,
    baseInfo,
    getUserInfo,
    getLoginInfo
  };
});